package com.quanfaqiang.demo1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanfaqiang.demo1.mapper.UserMapper;
import com.quanfaqiang.demo1.pojo.User;
import com.quanfaqiang.demo1.service.UserService;
import com.quanfaqiang.demo1.utils.Md5Util;
import com.quanfaqiang.demo1.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate stringRedisTemplate;

    @Override
    public User findByUserName(String username) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
    }

    @Override
    public void register(String username, String password) {
        String md5String = Md5Util.getMD5String(password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(md5String);
        userMapper.insert(user);
    }

    @Override
    public boolean logoff(String username, String password) {
        int delete = userMapper.delete(new QueryWrapper<User>().eq("username", username).eq("password", Md5Util.getMD5String(password)));
        if (delete > 0) {
            Boolean token = stringRedisTemplate.delete("token");
            if (token)
            {
                ThreadLocalUtil.remove();
            }
            return true;
        }
        return false;
    }

    @Override
    public Page<User> SelectAll(int currentPage, int pageSize) {
        Page<User> page = new Page<>(currentPage, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Page<User> UserPage = userMapper.selectPage(page, queryWrapper);
        System.out.println("当前页:" + UserPage.getCurrent());
        System.out.println("每页记录数:" + UserPage.getSize());
        System.out.println("总记录数:" + UserPage.getTotal());
        System.out.println("总页数:" + UserPage.getPages());
        List<User> UserList = UserPage.getRecords();
        UserList.forEach(System.out::println);
        return UserPage;
    }

    @Override
    public void DeleteId(Integer id) {
        userMapper.deleteById(id);
    }


    @Override
    public void deleteIds(List<Integer> ids) {
        userMapper.deleteBatchIds(ids);
    }



    @Override
    public User SelectId(Integer id) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("id", id));
    }

    @Override
    public void Add(User user) {
        user.setPassword(Md5Util.getMD5String(user.getPassword()));
        userMapper.insert(user);
    }

    @Override
    public Boolean selectPassword(String password, Integer id) {
        User user = userMapper.selectById(id);
        if (user != null) {
            if (Md5Util.getMD5String(password).equals(user.getPassword())) {
                return true;
            }
            return false;
        }
        return false;

    }

    @Override
    public void update(User user) {
        user.setPassword(Md5Util.getMD5String(user.getPassword()));
        userMapper.updateById(user);
    }

    @Override
    public void updatePwd(String password, Integer id) {
        System.out.println(password+":"+id);
        String md5String = Md5Util.getMD5String(password);
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userLambdaUpdateWrapper.eq(User::getId, id);
        userLambdaUpdateWrapper.set(User::getPassword, md5String);
        userMapper.update(userLambdaUpdateWrapper);
//        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
//        stringObjectHashMap.put("id", id);
//        User user1 = userMapper.selectById(id);
//        stringObjectHashMap.put("username",user1.getUsername() );
//        String token = JwtUtil.genToken(stringObjectHashMap);
//        //把token存储到redisz中
//        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
//        operations.set("token",token);
//        //把业务数据储存为ThreaddLocal中
//        Map<String, Object> claims = JwtUtil.parseToken(token);
//        ThreadLocalUtil.set(claims);
    }

    @Override
    public void updateUserAddress(String userAddress, int id) {
        LambdaUpdateWrapper<User> eq = new LambdaUpdateWrapper<User>().eq(User::getAddress, userAddress).eq(User::getId, id);
        System.out.println(eq);
        userMapper.update(eq);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        System.out.println(id+":"+avatarUrl);
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userLambdaUpdateWrapper.eq(User::getId, id);
        userLambdaUpdateWrapper.set(User::getUserpic, avatarUrl);
        userMapper.update(userLambdaUpdateWrapper);
        System.out.println("===============");
    }



    @Override
    public Page<User> searchUser(String userName, Integer currentPage, Integer pageSize) {
        Page<User> page = new Page<>(currentPage, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Page<User> userPage = userMapper.selectPage(page, queryWrapper.like("username",userName));
        return userPage;
    }

    @Override
    public Boolean selectUsername(String username) {
        List<User> username1 = userMapper.selectList(new QueryWrapper<User>().eq("username", username));
        System.out.println(username1);
        if (username1.size() > 0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public User selectUsername1(String username) {
        User username1 = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        return username1;

    }

    @Override
    public void updateUserStatus(int i, String online) {
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userLambdaUpdateWrapper.eq(User::getId, i);
        if (online=="online"){
            userLambdaUpdateWrapper.set(User::isIsonline,true);
        }
        if (online=="offline"){
            userLambdaUpdateWrapper.set(User::isIsonline,false);
        }
        userMapper.update(userLambdaUpdateWrapper);
    }

    @Override
    public Page<User> serchUser(String username, String address,String name, String phone, String email, Boolean isadmin, Boolean isonline, Integer currentPage, Integer pageSize) {
        Page<User> page = new Page<>(currentPage, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (name != null && !"".equals(name)) {
            queryWrapper.like("name", name);
        }
        if (username != null) {
            queryWrapper.like("username", username);
        }
        if (address != null && !"".equals(address)) {
            queryWrapper.like("address", address);
        }
        if (email != null && !"".equals(email)) {
            queryWrapper.like("email", email);
        }
        if (phone != null && !"".equals(phone)) {
            queryWrapper.like("phone", phone);
        }
        if (isadmin != null && !"".equals(isadmin)) {
            queryWrapper.eq("isadmin", isadmin);
        }
        if (isonline != null && !"".equals(isonline)) {
            queryWrapper.eq("isonline", isonline);
        }

        Page<User> UserPage = userMapper.selectPage(page, queryWrapper);
        return UserPage;
    }


}
